package cn.sc.summer.seata.runner;

import cn.sc.summer.seata.service.SeataService;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.exception.NacosException;
import io.seata.spring.boot.autoconfigure.properties.SeataProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类名：seata配置文件销毁
 *
 * @author a-xin
 * @date 2023/8/25 14:31
 */
@Slf4j
@Component
public class SeataRunner implements ApplicationListener<ContextClosedEvent> {

    @Resource
    protected NacosConfigProperties nacosConfigProperties;

    @Resource
    protected SeataProperties seataProperties;

    @Override
    public void onApplicationEvent(@NonNull ContextClosedEvent event) {
        try {
            SeataService.deleteSeataConfig(seataProperties, nacosConfigProperties);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

}
